package handler

import (
	"fmt"
	"golang.org/x/exp/rand"
	"gorm.io/gorm"
	"time"
)

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page <= 0 {
			page = 1
		}
		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}
		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
func GenerateOrderSn(userId int32) string {
	//订单号的生成规则：年月日时分秒+用户id+2位随机数
	now := time.Now()
	rand.Seed(uint64(time.Now().UnixNano()))
	orderSn := fmt.Sprintf("%d%d%d%d%d%d%d%d", now.Year(), now.Month(), now.Day(), now.Hour(),
		now.Minute(), now.Second(), userId, rand.Intn(90)+10)
	return orderSn
}
